package com.health.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.health.common.Result;
import com.health.entity.BannedUsers;
import com.health.entity.User;
import com.health.entity.pojo.dto.AdminDTO;
import com.health.entity.pojo.dto.BanDTO;
import com.health.entity.pojo.dto.QueryBanDTO;
import com.health.entity.pojo.vo.QueryAdminVO;
import com.health.service.IBannedUsersService;
import com.health.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sanchuan
 * @since 2023-04-22
 */
@Api(tags = "封禁管理")
@RestController
@RequestMapping("/bannedUsers")
public class BannedUsersController extends BaseController {

    @Autowired
    private IBannedUsersService bannedUsersService;

    @Autowired
    private IUserService userService;

    @ApiOperation("封禁用户")
    @PostMapping("/banUser")
    @Transactional
    public Result<Boolean> banUser(@RequestBody BanDTO banDTO) {
        System.out.println(banDTO);
        Boolean result = false;
        //获得封禁帐号
        List<Integer> ids = new ArrayList<>();
        List<BannedUsers> banUsers = new ArrayList<>();
        //循环拿到每个封禁对象批量写入banned_users表
        banDTO.getUsernames().forEach(item -> {
            BannedUsers bannedUsers = new BannedUsers();
            ids.add(Math.toIntExact(item.getId()));
            bannedUsers.setUserId(Math.toIntExact(item.getId()));
            bannedUsers.setUsername(item.getUsername());
            System.out.println(item.getPhone());
            bannedUsers.setPhone(item.getPhone());
            bannedUsers.setContent(banDTO.getContent());
            bannedUsers.setPhone(item.getPhone());
            if (banDTO.getType().equals("1")){
                bannedUsers.setBanTime(banDTO.getDays());
                banUsers.add(bannedUsers);
            } else if (banDTO.getType().equals("2")) {
                bannedUsers.setBanTime(-1);
                banUsers.add(bannedUsers);
            }
        });
        userService.banUserForever(1, ids);
        result = bannedUsersService.saveBatch(banUsers);
        return this.dataResult(result);
    }

    @ApiOperation("解封用户")
    @PostMapping("/UnblockingUsers")
    @Transactional
    public Result<Boolean> UnblockingUsers(@RequestBody List<BanDTO> banDTOs) {
        System.out.println(banDTOs);
        Boolean result = false;
        //获得解封帐号
        List<Integer> ids = new ArrayList<>();
        List<Integer> unBanUsers = new ArrayList<>();
        //循环拿到每个解封对象批量写入banned_users表
        banDTOs.forEach(item -> {
            ids.add(item.getUserId());
            unBanUsers.add(item.getId());
        });
        System.out.println(ids);
        System.out.println(unBanUsers);
        userService.banUserForever(0, ids);
        result = bannedUsersService.removeByIds(unBanUsers);
        return this.dataResult(result);
    }

    @ApiOperation("获得封禁列表分页+条件查询")
    @PostMapping("/listBanUsersPage/{pageNum}/{pageSize}")
    public Result listBanUsersPage(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestBody QueryBanDTO queryBanDTO){
        IPage<BannedUsers> bannedUsers = bannedUsersService.listBanUsersPage(pageNum, pageSize, queryBanDTO);
        return this.dataResult(bannedUsers);
    }

    @ApiOperation("根据id解除封禁")
    @GetMapping("/UnblockingUserById/{id}/{userId}")
    @Transactional
    public Result<Boolean> UnblockingUserById(@PathVariable("id") Integer id,@PathVariable("userId") Integer userId){
        bannedUsersService.removeById(id);
        //相应的更新user表is_lock状态
        Boolean result = userService.unblockingUser(userId);
        return this.dataResult(result);
    }
}
